package cn.bonoon.kernel.security;

import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;

public class ProxyLogonUser implements LogonUserProxyable{
	
	private static final long serialVersionUID = -6174788620332332130L;

	private LogonUser source;
	
	/** 是否已经过期，代管别人的账号可能需要指定一个过期的时间 */
	private boolean overdued = false;
	
	/** 这个是可以代管别人账号的用户，这个用户是不允许被改变的 */
	private final LogonUser logonUser;
	
	public ProxyLogonUser(LogonUser logonUser){
		this.logonUser = logonUser;
		
		this.source = logonUser;
	}
	
	@Override
	public LogonUserRuntime getRuntime() {
		return logonUser.getRuntime();
	}
	
	public void overdued(){
		overdued = true;
	}
	
	@Override
	public boolean isActing() {
		return logonUser != source && logonUser.isSuper();
	}
	
	@Override
	public boolean isProxyable() {
		return !overdued;
	}
	
	@Override
	public LogonUser getLogonUser() {
		return logonUser;
	}
	
	@Override
	public LogonUser getSourceUser() {
		return source;
	}
	
	@Override
	public void proxy(LogonUser source) {
		this.source = source;
	}
	
	@Override
	public void reset() {
		this.source = logonUser;
	}
	
	// ********************下面是代理 "IOperator source" 的方法
	
	public void set(String name, Object obj) {
		source.set(name, obj);
	}

	public String getDisplayName() {
		String value = source.getDisplayName();
		if(isActing()){
			value += "<span title='代理者:" + logonUser.getDisplayName() 
					+ "[" + logonUser.getUsername() + "]'>@</span>";
		}
		return value;
	}

	public Long getId() {
		return source.getId();
	}

	public String getUsername() {
		return source.getUsername();
	}

	public Long getOwnerId() {
		return source.getOwnerId();
	}

	public long toOwnerId() {
		return source.toOwnerId();
	}

	public Long encryptId(Long id) {
		return source.encryptId(id);
	}

	public long encryptId(long id) {
		return source.encryptId(id);
	}

	public Long decryptId(Long id) {
		return source.decryptId(id);
	}

	public long decryptId(long id) {
		return source.decryptId(id);
	}

	public boolean isSuper() {
		return source.isSuper();
	}

	public boolean isAdmin() {
		return source.isAdmin();
	}

	public Object get(String name) {
		return source.get(name);
	}

	public Collection<? extends GrantedAuthority> getAuthorities() {
		return source.getAuthorities();
	}

	public String getPassword() {
		return source.getPassword();
	}

	public boolean isAccountNonExpired() {
		return source.isAccountNonExpired();
	}

	public boolean isAccountNonLocked() {
		return source.isAccountNonLocked();
	}

	public boolean isCredentialsNonExpired() {
		return source.isCredentialsNonExpired();
	}

	public boolean isEnabled() {
		return source.isEnabled();
	}

	public boolean allowAccess() {
		return source.allowAccess();
	}

	@Override
	public void checkEditable() throws EditableException {
		source.checkEditable();
	}
	
//	@Override
//	public LogonUser set(String name, Object obj) {
//		source.set(name, obj);
//		return this;
//	}

	public boolean needChangePwd() {
		//代理别人的账号不需要修改密码
		return false;
	}

//	public void changePwd() {
//		source.changePwd();
//	}

//	@Override
//	public void loginSuccess(String ip) {
//		source.loginSuccess(ip);
//	}
//	
//	@Override
//	public String getIp() {
//		return source.getIp();
//	}
//	
//	@Override
//	public String getLoginAt() {
//		return source.getLoginAt();
//	}
}
